import {defineStore} from 'pinia';

const useTagsViewStore = defineStore('tags-view', {
	state: () => ({
		// VisitedViews: [] as Array<IVistView>,
		visitedViews: [] as any[],
		cachedViews: [] as any[],
	}),
	actions: {
		addView(view: any) {
			this.addVisitedView(view);
			this.addCachedView(view);
		},
		// prettier-ignore
		addVisitedView(view: {path: string; meta: {title: string}}) {
			if (this.visitedViews.some((v: {path: string}) => v.path === view.path)) {
				return;
			}

			this.visitedViews.push(
				{...view, title: view.meta.title || 'no-name'},
			);
		},
		addCachedView(view: any) {
			if (this.cachedViews.includes(view.name)) {
				return;
			}

			if (!view.meta.noCache) {
				this.cachedViews.push(view.name);
			}
		},
		async delView(view: any) {
			return new Promise(resolve => {
				this.delVisitedView(view);
				this.delCachedView(view);
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews],
				});
			});
		},
		async delVisitedView(view: {path: string}) {
			return new Promise(resolve => {
				for (const [i, v] of this.visitedViews.entries()) {
					if (v.path === view.path) {
						this.visitedViews.splice(i, 1);
						break;
					}
				}

				resolve([...this.visitedViews]);
			});
		},
		async delCachedView(view: any) {
			return new Promise(resolve => {
				const index = this.cachedViews.indexOf(view.name);
				index > -1 && this.cachedViews.splice(index, 1);
				resolve([...this.cachedViews]);
			});
		},
		async delOthersViews(view: any) {
			return new Promise(resolve => {
				this.delOthersVisitedViews(view);
				this.delOthersCachedViews(view);
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews],
				});
			});
		},
		async delOthersVisitedViews(view: {path: string}) {
			return new Promise(resolve => {
				this.visitedViews = this.visitedViews.filter(v => v.meta.affix || v.path === view.path);
				resolve([...this.visitedViews]);
			});
		},
		async delOthersCachedViews(view: {name: string}) {
			return new Promise(resolve => {
				const index = this.cachedViews.indexOf(view.name);
				if (index > -1) {
					this.cachedViews = this.cachedViews.slice(index, index + 1);
				} else {
					this.cachedViews = [];
				}

				resolve([...this.cachedViews]);
			});
		},
		async delAllViews(view?: any) {
			return new Promise(resolve => {
				this.delAllVisitedViews(view);
				this.delAllCachedViews(view);
				resolve({
					visitedViews: [...this.visitedViews],
					cachedViews: [...this.cachedViews],
				});
			});
		},
		async delAllVisitedViews(view: any) {
			return new Promise(resolve => {
				// prettier-ignore
				const affixTags = this.visitedViews.filter(tag => tag.meta.affix);
				this.visitedViews = affixTags;
				resolve([...this.visitedViews]);
			});
		},
		async delAllCachedViews(view: any) {
			return new Promise(resolve => {
				this.cachedViews = [];
				resolve([...this.cachedViews]);
			});
		},
		updateVisitedView(view: {path: string}) {
			for (let v of this.visitedViews) {
				if (v.path === view.path) {
					v = Object.assign(v, view);
					break;
				}
			}
		},
		async delRightTags(view: {path: string}) {
			return new Promise(resolve => {
				// prettier-ignore
				const index = this.visitedViews.findIndex((v: {path: string}) => v.path === view.path);
				if (index === -1) {
					return;
				}

				this.visitedViews = this.visitedViews.filter((item, idx) => {
					if (idx <= index || (item.meta?.affix)) {
						return true;
					}

					const i = this.cachedViews.indexOf(item.name);
					if (i > -1) {
						this.cachedViews.splice(i, 1);
					}

					return false;
				});
				resolve([...this.visitedViews]);
			});
		},
		async delLeftTags(view: {path: string}) {
			return new Promise(resolve => {
				// prettier-ignore
				const index = this.visitedViews.findIndex((v: {path: string}) => v.path === view.path);
				if (index === -1) {
					return;
				}

				this.visitedViews = this.visitedViews.filter((item, idx) => {
					if (idx >= index || (item.meta?.affix)) {
						return true;
					}

					const i = this.cachedViews.indexOf(item.name);
					if (i > -1) {
						this.cachedViews.splice(i, 1);
					}

					return false;
				});
				resolve([...this.visitedViews]);
			});
		},
	},
});

export default useTagsViewStore;
